//package com.evil.gateway.config;
//
//import com.evil.common.core.enums.RCodeEnum;
//import org.springframework.boot.web.error.ErrorAttributeOptions;
//import org.springframework.boot.web.reactive.error.DefaultErrorAttributes;
//import org.springframework.core.annotation.MergedAnnotation;
//import org.springframework.core.annotation.MergedAnnotations;
//import org.springframework.http.HttpStatus;
//import org.springframework.stereotype.Component;
//import org.springframework.web.bind.annotation.ResponseStatus;
//import org.springframework.web.reactive.function.server.ServerRequest;
//import org.springframework.web.server.ResponseStatusException;
//
//import java.util.Date;
//import java.util.HashMap;
//import java.util.Map;
//
///**
// * @author liyang
// * @date 2023-02-01 15:04
// */
//@Component
//public class GlobalErrorAttributes extends DefaultErrorAttributes {
//
//    @Override
//    public Map<String, Object> getErrorAttributes(ServerRequest request, ErrorAttributeOptions options) {
//        Map<String, Object> errorAttributes = super.getErrorAttributes(request, options);
//        Throwable error = this.getError(request);
//        String exMessage = errorAttributes != null ? error.getMessage() : RCodeEnum.ERROR.getMessage();
//        String message = String.format("请求失败 [%s %s]，异常：%s", request.methodName(), request.uri(), exMessage);
//
//        if (null != errorAttributes) {
//            MergedAnnotation<ResponseStatus> responseStatusAnnotation = MergedAnnotations
//                    .from(error.getClass(), MergedAnnotations.SearchStrategy.TYPE_HIERARCHY).get(ResponseStatus.class);
//            HttpStatus errorStatus = determineHttpStatus(error, responseStatusAnnotation);
//
//            //必须设置, 否则会报错, 因为 DefaultErrorWebExceptionHandler 的 renderErrorResponse 方法会获取此属性, 重新实现 DefaultErrorWebExceptionHandler也可.
//            errorAttributes.put("status", errorStatus.value());
//            errorAttributes.put("code", errorStatus.value());
//
//            //html view用
//            errorAttributes.put("timestamp", new Date());
//            //html view 用
//            errorAttributes.put("requestId", request.exchange().getRequest().getId());
//
//            errorAttributes.put("error", errorStatus.getReasonPhrase());
//            errorAttributes.put("exception", error.getClass().getName());
//        }
//
//        Map<String, Object> map = new HashMap<>(3);
//        map.put("code", RCodeEnum.ERROR.getCode());
//        map.put("message", message);
//        map.put("data", null);
//        return map;
//    }
//
//    //从DefaultErrorWebExceptionHandler中复制过来的
//    private HttpStatus determineHttpStatus(Throwable error, MergedAnnotation<ResponseStatus> responseStatusAnnotation) {
//        if (error instanceof ResponseStatusException) {
//            return ((ResponseStatusException) error).getStatus();
//        }
//        return responseStatusAnnotation.getValue("code", HttpStatus.class).orElse(HttpStatus.INTERNAL_SERVER_ERROR);
//    }
//}